import jieba
from matplotlib import pylab as plt
from wordcloud import WordCloud
from PIL import Image
import numpy as np
from pymysql import *
import json


def get_img(field, targetImageSrc, resImageSrc):
    # 连接数据库
    con = connect(host='localhost', user='root', passwd='123456', db='bossinfo', port=3306, charset='utf8mb4')
    cursor = con.cursor()
    sql = f'select {field} from jobinfo '
    cursor.execute(sql)
    data = cursor.fetchall()

    # 获取数据库中所有的福利词
    text = ''
    if field == 'companyTags':
        for i, item in enumerate(data):
            if item[0] != '无':
                tags = json.loads(item[0])
                for j in tags:
                    text += j
    else:
        for i, item in enumerate(data):
            text += item[0]

    cursor.close()
    con.close()

    # 对福利词进行停止词处理
    data_cut = jieba.cut(text, cut_all=False)
    # stop_words = []
    # with open('./stopwords.txt', 'r', encoding='utf-8') as rf:
    #     for line in rf:
    #         if len(line) >0 :
    #             stop_words.append(line.strip())
    #
    # data_result = [x for x in data_cut if x not in stop_words]
    string = ' '.join(data_cut)

    # 图片
    img = Image.open(targetImageSrc)
    img_arr = np.array(img)
    wc = WordCloud(background_color='white', font_path='STHUPO.TTF', mask=img_arr)
    wc.generate_from_text(string)

    # 绘制图片
    fig = plt.figure(1)
    plt.imshow(wc)
    plt.axis('off')
    plt.savefig(resImageSrc, dpi=500)


if __name__ == '__main__':
    get_img('companyTags', '../static/1.jpg', '../static/companyTags_cloud.png')
    get_img('companyTitle', '../static/2.jpg', '../static/title_cloud.png')
